package com.atXiao.food.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class IpLocationDemo {
    public static void main(String[] args) {
        try {
            String apiKey = "0113a13c88697dcea6a445584d535837";
            String ip = "39.144.5.166";
            String apiUrl = "https://restapi.amap.com/v3/ip?key=" + apiKey + "&ip=" + ip;

            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            reader.close();
            connection.disconnect();

            // 解析JSON响应
            // 请使用您项目中的 JSON 解析库来解析 response 字符串，这里只是演示
            String jsonString = response.toString();
            System.out.println(jsonString);

            // 假设使用 JSON 解析库解析为一个对象，例如：
            // JsonObject jsonObject = YourJsonParser.parse(jsonString);
            // String city = jsonObject.getString("city");
            // String cityCode = jsonObject.getString("adcode");
            // String province = jsonObject.getString("province");
            // String provinceCode = jsonObject.getString("provinceCode");
            // String rectangle = jsonObject.getString("rectangle");

            // 输出解析结果
            // System.out.println("City: " + city);
            // System.out.println("City Code: " + cityCode);
            // System.out.println("Province: " + province);
            // System.out.println("Province Code: " + provinceCode);
            // System.out.println("Rectangle: " + rectangle);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
